<?php
class RiaDoctrine_Record extends Doctrine_Record{
	const TRIGGER_TYME_BEFORE  = 1;
	const TRIGGER_TYME_AFTER   = 2;
	const TRIGGER_EVENT_INSERT = 1;
	const TRIGGER_EVENT_UPDATE = 2;
	const TRIGGER_EVENT_DELETE = 3;
	
	
	private $triggers =  array();
	/**
     * hasTrigger
     * sets a trigger definition
     *
     * @param string  $name
     * @param integer $time
     * @param integer $event
     * @param mixed $dql
     * @return void
     */
	public function hasTrigger($name,$time,$event,$dql){
		$name = strtolower($name);
		$arr = $this->getTable()->triggers;
		$arr[$name] = array('time' => $time, 'event' => $event, 'dql' => $dql);
		$this->getTable()->triggers = $arr;
	}
	
	/**
     * getSqlTrigger
     * get a trigger sql
     *
     * @param string  $name
     * @return string
     */
	
	public function getSqlTrigger($name){
		$name = strtolower($name);
		$triggers_arr = $this->getTable()->triggers;
		if (is_array($triggers_arr[$name])){
			
			$time_sql = '';
			
			switch ($triggers_arr[$name]['time']){
				case 1:
					$time_sql = 'BEFORE';
				break;
				case 2:
					$time_sql = 'AFTER';
				break;
				default:
					return false;
				break;
			}
			
			$event_sql = '';
			
			switch ($triggers_arr[$name]['event']){
				case 1:
					$event_sql = 'INSERT';
				break;
				case 2:
                    $event_sql = 'UPDATE';
                break;
                case 3:
                    $event_sql = 'DELETE';
                break;
			}
			
			$table_name = $this->getTable()->getTableName();
			$database = '';
			if (strpos($table_name,'.') !== false){
				$database = substr($table_name,0,strpos($table_name,'.')+1);
			}
			
			$exec_sql = '';
			$dql = $triggers_arr[$name]['dql'];
			
			if(!is_array($dql)){
				$dql = array($dql);
			}
			
			foreach ($dql as $item){
				if ($item instanceof Doctrine_Query){
				    $exec_sql .= $item->getSql();
				}else{
					$exec_sql .= 'INSERT INTO ' . $item->getTable()->getTableName(). ' SET ';
					$field_data = $item->getPrepared();
					foreach ($field_data as $key=>$item2){
						$cols = $item->getTable()->getColumnName($key);
						if (stripos($item2,'new.')!==false){
						  $exec_sql.= "$cols=$item2, ";
						}else{
						  $exec_sql.= "$cols='$item2', ";
						}
					}
					$exec_sql = substr($exec_sql,0,strlen($exec_sql)-2);
				}
				$exec_sql .= ';';
			}
			
			$sql = "CREATE TRIGGER $database$name $time_sql $event_sql
                    ON $table_name FOR EACH ROW BEGIN $exec_sql END;
                    ";
            return $sql;   

		}
		return false;
	}
	
	/**
     * getTriggers
     * get a trigger sql
     *
     * @return array
     */
	
	public function getTriggers(){
		$arr = array();
		foreach ($this->triggers as $key=>$item){
			array_push($arr,$key);
		}
		return array();
	}
	
    /**
     * create
     * returns a new Doctrine_Query object
     *
     * @param Doctrine_Connection $conn     optional connection parameter
     * @return Doctrine_Query
     */
    
    public function getQuery($conn = null){
        return Doctrine_Query::create($conn);
    }
}
?>